from datetime import datetime
class fun_list:
    def is_number(self, s):
        try:
            float(s)
            return True
        except ValueError:
            return False


    def is_valid_id(self, id_number):
        import re
        # 检查身份证号是否为18位且仅包含数字及最后一位可能为字母X
        if len(id_number) != 18:
            return False  # "身份证号错误!"
        else:
            pattern = r'^\d{17}[\dX]$'
            # print(re.match(pattern, id_number))
            if re.match(pattern, id_number) is None:
                return False
            # 十七位数字本体码权重
            weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
            # mod11, 对应校验码字符值
            validate = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
            sum = 0
            num = 0
            for sfz in id_number[0:-1]:
                sum = sum + int(sfz) * weight[num]
                num = num + 1
            model = sum % len(validate)

            if validate[model] == id_number[-1:]:
                return True  #
                # print("身份证号正确")
            else:
                return False  #
                # print("身份证号错误")


    # 定义日期格式验证函数
    def is_valid_date(self, date_string):

        # 本次校验都是6位数据
        if len(date_string) != 6:
            # print(f'【{date_string}】日期字符不是日期格式')
            return False
        formats = ["%Y%m"]
        for fmt in formats:
            try:
                d = datetime.strptime(date_string, fmt)
                # print(d)
                return True
            except ValueError:
                continue
        return False


    # 定义日期格式验证函数
    def is_valid_date8(self, date_string):

        # 本次校验都是8位数据
        if len(date_string) != 8:
            # print(f'【{date_string}】日期字符不是日期格式')
            return False
        formats = ["%Y%m%d"]
        for fmt in formats:
            try:
                d = datetime.strptime(date_string, fmt)
                # print(d)
                return True
            except ValueError:
                continue
        return False


if __name__ == '__main__':

    # 创建主窗口并运行
    t = fun_list()
    a = t.is_valid_date8('33324422')
    print(a)
